#define _WIN32_DCOM
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <ocidl.h>

//DFFE86C9-ED1D-11D2-BB52-006097B5EAFC
const CLSID CLSID_PersistentJava = {0xDFFE86C9, 0xED1D, 0x11D2, {0xBB, 0x52, 0x00, 
    0x60, 0x97, 0xB5, 0xEA, 0xFC}};

void main()
{
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
    if(FAILED(hr))
        cout << "CoInitializeEx failed. " << endl;

    IUnknown* pUnknown;
   hr = CoCreateInstance(CLSID_PersistentJava, NULL, CLSCTX_INPROC_SERVER, 
      IID_IUnknown, (void**)&pUnknown);
    if(FAILED(hr))
      cout << "CoCreateInstance failed. " << endl;

    IPersistStreamInit* pPersistStreamInit;
    hr = pUnknown->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit);
    if(FAILED(hr))
        cout << "IID_IPersistStreamInit not supported. " << endl;




	IStream* pStream = 0;
	hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    if(FAILED(hr))
        cout << "CreateStreamOnHGlobal failed. " << endl;

	hr = pPersistStreamInit->InitNew();
   if(FAILED(hr))
     printf("IPersistStreamInit::InitNew failed %0x\n", hr);


	hr = pPersistStreamInit->Save(pStream, TRUE);
   if(FAILED(hr))
     printf("IPersistStreamInit::Save failed %0x\n", hr);


   LARGE_INTEGER zero = { 0, 0 };
   pStream->Seek(zero, STREAM_SEEK_SET, NULL);


    IUnknown* pUnknown2;
   hr = CoCreateInstance(CLSID_PersistentJava, NULL, CLSCTX_INPROC_SERVER, 
      IID_IUnknown, (void**)&pUnknown2);
    if(FAILED(hr))
      cout << "CoCreateInstance failed. " << endl;
 
    IPersistStreamInit* pPersistStreamInit2;
    hr = pUnknown2->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit2);
    if(FAILED(hr))
        cout << "IID_IPersistStreamInit not supported. " << endl;

	hr = pPersistStreamInit2->Load(pStream);
    if(FAILED(hr))
       printf("IPersistStreamInit::Load failed %0x\n", hr);

    pPersistStreamInit2->Release();
    pUnknown2->Release();

    pPersistStreamInit->Release();
	pStream->Release();
    pUnknown->Release();

    CoUninitialize();
}